#coding:utf-8
import os
import time
import datetime
from optparse import OptionParser
import shutil
import glob

file_list = [
    "96.0.4664.137.manifest",
    "chrome_100_percent.pak",
    "chrome_200_percent.pak",
    "resources.pak",
    "v8_context_snapshot.bin",
    "icudtl.dat",
    "96.0.4664.137.manifest",
    "api-ms-win-core-console-l1-1-0.dll",
    "api-ms-win-core-console-l1-2-0.dll",
    "api-ms-win-core-datetime-l1-1-0.dll",
    "api-ms-win-core-debug-l1-1-0.dll",
    "api-ms-win-core-errorhandling-l1-1-0.dll",
    "api-ms-win-core-file-l1-1-0.dll",
    "api-ms-win-core-file-l1-2-0.dll",
    "api-ms-win-core-file-l2-1-0.dll",
    "api-ms-win-core-handle-l1-1-0.dll",
    "api-ms-win-core-heap-l1-1-0.dll",
    "api-ms-win-core-interlocked-l1-1-0.dll",
    "api-ms-win-core-libraryloader-l1-1-0.dll",
    "api-ms-win-core-localization-l1-2-0.dll",
    "api-ms-win-core-memory-l1-1-0.dll",
    "api-ms-win-core-namedpipe-l1-1-0.dll",
    "api-ms-win-core-processenvironment-l1-1-0.dll",
    "api-ms-win-core-processthreads-l1-1-0.dll",
    "api-ms-win-core-processthreads-l1-1-1.dll",
    "api-ms-win-core-profile-l1-1-0.dll",
    "api-ms-win-core-rtlsupport-l1-1-0.dll",
    "api-ms-win-core-string-l1-1-0.dll",
    "api-ms-win-core-synch-l1-1-0.dll",
    "api-ms-win-core-synch-l1-2-0.dll",
    "api-ms-win-core-sysinfo-l1-1-0.dll",
    "api-ms-win-core-timezone-l1-1-0.dll",
    "api-ms-win-core-util-l1-1-0.dll",
    "api-ms-win-crt-conio-l1-1-0.dll",
    "api-ms-win-crt-convert-l1-1-0.dll",
    "api-ms-win-crt-environment-l1-1-0.dll",
    "api-ms-win-crt-filesystem-l1-1-0.dll",
    "api-ms-win-crt-heap-l1-1-0.dll",
    "api-ms-win-crt-locale-l1-1-0.dll",
    "api-ms-win-crt-math-l1-1-0.dll",
    "api-ms-win-crt-multibyte-l1-1-0.dll",
    "api-ms-win-crt-private-l1-1-0.dll",
    "api-ms-win-crt-process-l1-1-0.dll",
    "api-ms-win-crt-runtime-l1-1-0.dll",
    "api-ms-win-crt-stdio-l1-1-0.dll",
    "api-ms-win-crt-string-l1-1-0.dll",
    "api-ms-win-crt-time-l1-1-0.dll",
    "api-ms-win-crt-utility-l1-1-0.dll",
    "api-ms-win-downlevel-kernel32-l2-1-0.dll",
    "api-ms-win-eventing-provider-l1-1-0.dll",
    "chrome.dll",
    "chrome_elf.dll",
    "d3dcompiler_47.dll",
    "dbgcore.dll",
    "dbghelp.dll",
    "libEGL.dll",
    "libGLESv2.dll",
    "mojo_core.dll",
    "msvcp140.dll",
    "ucrtbase.dll",
    "vccorlib140.dll",
    "vcruntime140.dll",
    "vcruntime140_1.dll",
    "VkICD_mock_icd.dll",
    "VkLayer_khronos_validation.dll",
    "vk_swiftshader.dll",
    "vulkan-1.dll",
    "chrome.exe",
    "chrome_proxy.exe",
    "locales\\*",
    "MEIPreload\\*",
    "swiftshader\\*.dll",
    "hscore\\*"
]

if __name__ == '__main__':
    parser = OptionParser(usage="%prog [options]")
    parser.add_option("-d", "--dir", action="store", type="string", dest="dir_build", help=u"指定编译目录")
    (options, args) = parser.parse_args()
    if options.dir_build is None:
        print("请用-d或者--dir指定chromium的编译目录。")
        exit(0)
    chromium_build_dir = options.dir_build

    dist_dir = "D:\\chromium_dist"
    if not os.path.exists(dist_dir):
        os.mkdir(dist_dir)

    tm = time.localtime() #(tm_year,tm_mon,tm_mday,tm_hour,tm_min, tm_sec,tm_wday,tm_yday,tm_isdst)
    dist_dir += "\\pack_{0}-{1}-{2}-{3}-{4}-{5}".format(tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec)
    #print(dist_dir)
    os.mkdir(dist_dir)

    for i in file_list:
        src_is_file = src_is_folder = src_is_specified_file_under_folder = False
        if not i.find("*") != -1:
            src_is_file = True
        elif i.endswith("\\*"):
            src_is_folder = True
        else:
            src_is_specified_file_under_folder = True

        if src_is_file:
            # if i.find("\\"): file_in_sub_folder = true; sub_dir_list = i.split("\\") ...
            src_file = os.path.join(chromium_build_dir, i)
            shutil.copy(src_file, dist_dir)
        elif src_is_folder:
            src_folder_name = i[0:-2]
            src_dir = os.path.join(chromium_build_dir, src_folder_name)
            shutil.copytree(src_dir, dist_dir + "\\" + src_folder_name)
        elif src_is_specified_file_under_folder:
            lst = i.split("\\")
            src_dir = chromium_build_dir
            to_dir = dist_dir
            file_name_with_patten = ""
            for k in lst:
                if k.find("*") == -1:
                    src_dir = os.path.join(chromium_build_dir, k)
                    to_dir = os.path.join(dist_dir, k)
                    if not os.path.exists(to_dir):
                        os.mkdir(to_dir)
                else:
                    file_name_with_patten = k
                    break
            if file_name_with_patten == "":
                print("LOGIC ERROR!")
                break
            fl = glob.glob(src_dir + "\\" + file_name_with_patten)
            #print(fl)
            for z in fl:
                shutil.copy(z, to_dir)
        else:
            print("LOGIC ERROR!")
            break

